#include <tiredis/Executor.h>
#include <tiredis/Statistics.h>

pingcap::kv::ClusterPtr Executor::tikv_cluster;

Executor::Executor()
{
}

void Executor::Init(const std::vector<std::string> & pd_addrs)
{
     pingcap::ClusterConfig config;
     config.learner_key = "engine";
     config.learner_value = "zone";
     tikv_cluster = std::make_unique<pingcap::kv::Cluster>(pd_addrs, config);
}

Error Executor::ExecuteCmd(const std::vector<std::string>& params, UnboundedBuffer* reply)
{
    g_stat->totalRC_ ++;
    if (params.empty())
    {
        ReplyError(Error_param, reply);
        return Error_param;
    }

    auto it(CommandTable::s_handlers.find(params[0]));
    if (it == CommandTable::s_handlers.end())
    {
        ReplyError(Error_unknowCmd, reply);
        return Error_unknowCmd;
    }

    const CommandInfo* info = it->second;
    if(!info->CheckParamsCount(static_cast<int>(params.size())))
    {
        ReplyError(Error_param, reply);
        return Error_param;
    }

    return info->handler(params, reply);
}
